﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class TAB52
    {

        #region ==IsMU===========
        public static readonly DependencyProperty IsMUProperty = DependencyProperty.Register("IsMU",
            typeof(bool), typeof(TAB52),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsMUChanged));
        public bool IsMU
        {
            get
            {
                return (bool)GetValue(IsMUProperty);
            }
            set
            {
                SetValue(IsMUProperty, value);
            }
        }
        private static void OnIsMUChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB52;
            if (src != null)
            {
                src.IsMU = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==CART===========
        public static readonly DependencyProperty CARTProperty = DependencyProperty.Register("CART",
            typeof(string), typeof(TAB52),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCARTChanged));
        public string CART
        {
            get
            {
                return (string)GetValue(CARTProperty);
            }
            set
            {
                SetValue(CARTProperty, value);
            }
        }
        private static void OnCARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB52;
            if (src != null)
            {
                src.CART = (string)e.NewValue;
            }
        }
        #endregion
        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
            typeof(string), typeof(TAB52),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
                ORDERNUM = AUF;
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB52;
            if (src != null)
            {
                src.AUF = (string)e.NewValue;
                
            }
        }
        #endregion
        AniScrollViewer viewer
        {
            get
            {
                return (AniScrollViewer)this.FindName("MasterViewer");
            }
        }
        VICC_ABAX ABAX;
        VABPOS VABPOS;
        public TAB52()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABAUFM();
            ABAX = new VICC_ABAX();
            VABPOS = new VABPOS();
            this.DataContext = MV;
            InitializeComponent(); 
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            LOADBYAUF();
        }

       
        string LastAUF = "";
        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 12)
                return;
            dwData = TBMain.NewRow();
            string ordertype = ORDERNUM.Substring(0, 1);
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["ABAVKAUFKRS"] = ORDERNUM.Substring(1, 2);
            dwData["ABAVKAUFLFD"] = ORDERNUM.Substring(3, 6);
            dwData["ABAVKAUFPOS"] = ORDERNUM.Substring(9, 3);
            SResult = ((VABAUFM)MV).QueryABAUFM(dwData, 13);
            MergeData(SResult, true);
            string art, var, farbe, aufart;
            if (SResult.Tables[0].Rows.Count == 0)
            {
                switch (app.company)
                {
                    case "YGR":
                        SResult = ABAX.QueryICC_ABAX(dwData, 9);
                        SResult.Tables[0].TableName = TB;
                        MergeData(SResult, true);
                        break;
                    case "JLT":
                        aufart = AUF.Substring(12, 2);
                        if (aufart == "VA")
                        {
                            dwData = VABPOS.SetProd.Tables["ABPOS"].NewRow();
                            dwData["ABPMANDANT"] = app.Mandant;
                            dwData["ABPART"] = AUF.Substring(14, 20);
                            dwData["ABPVAR"] = AUF.Substring(34, 3);
                            dwData["ABPFARBE"] = AUF.Substring(37, 10);
                            dwData["ABPAUFART"] = "PR";
                            SResult = VABPOS.QueryABPOS(dwData, 32);
                            foreach (DataRow row in SResult.Tables[0].Rows)
                            {
                                row["ABAVKAUFKRS"] = AUF.Substring(1, 2);
                                row["ABAVKAUFLFD"] = AUF.Substring(3, 6);
                                row["ABAVKAUFPOS"] = AUF.Substring(9, 3);
                            }
                            SResult.Tables[0].TableName = TB;
                            MergeData(SResult, false, false);
                        }
                        if (aufart == "PR")
                        {
                            dwData = VABPOS.SetProd.Tables["ABPOS"].NewRow();
                            dwData["ABPMANDANT"] = app.Mandant;
                            dwData["ABPART"] = ORDERNUM.Substring(14, 20);
                            dwData["ABPVAR"] = "1  ";
                            dwData["ABPFARBE"] = ORDERNUM.Substring(37, 10);
                            dwData["ABPAUFART"] = "VW";
                            SResult = VABPOS.QueryABPOS(dwData, 32);
                            foreach (DataRow row in SResult.Tables[0].Rows)
                            {
                                row["ABAVKAUFKRS"] = ORDERNUM.Substring(1, 2);
                                row["ABAVKAUFLFD"] = ORDERNUM.Substring(3, 6);
                                row["ABAVKAUFPOS"] = ORDERNUM.Substring(9, 3);
                            }
                            SResult.Tables[0].TableName = TB;
                            MergeData(SResult, false, false);

                        }
                        if (aufart == "VW")
                        {

                            dwData = VABPOS.SetProd.Tables["ABPOS"].NewRow();
                            dwData["ABPMANDANT"] = app.Mandant;
                            dwData["ABPFARBE"] = AUF.Substring(14, 20).Replace("-", "").Trim();
                            dwData["ABPAUFART"] = "GP";
                            SResult = VABPOS.QueryABPOS(dwData, 33);
                            foreach (DataRow row in SResult.Tables[0].Rows)
                            {
                                row["ABAVKAUFKRS"] = ORDERNUM.Substring(1, 2);
                                row["ABAVKAUFLFD"] = ORDERNUM.Substring(3, 6);
                                row["ABAVKAUFPOS"] = ORDERNUM.Substring(9, 3);
                            }
                            SResult.Tables[0].TableName = TB;
                            MergeData(SResult, false, false);
                        }
                        break;
                }

            }

            Explore();//每个单，销售单，生产单会挂一个AB52，搜索前道订单，自动建立订单实例（TB50X）TB50X内包括TAB50和TAB52（这里是 TAB52）。这是递归算法
            //TAB50包括生产单信息和产量TGB13，TGB13计算产量，并归类。属于细纱？粗纱？分盘？回修？
            base.LOADBYAUF();
        }
        

        public void Explore()
        {
            int count = MasterMU.Children.Count;
            if (count > 0)
            {
                MasterMU.Children.RemoveRange(0, count);
            }
            IsMU = false;
            
            
            int idx = 0;
            TB50X TB50X;
            TAB05X TAB05X;
            TAB55X TAB55X;
            TAB45X TAB45X;

            foreach (DataRow mu in TBMain.Rows)
            {
                if (mu["ABAAUFTYP"].ToString() == "N") continue;
                string ABPAUFART = mu["ABPAUFART"].ToString();
                string _auf = "V" + mu["ABAAUFKRS"].ToString() + mu["ABAAUFLFD"].ToString() + mu["ABAAUFPOS"].ToString();
                switch (ABPAUFART)
                {
                    default:
                    case "GP":
                        TB50X = new TB50X();
                        TB50X.SetValue(Grid.RowProperty, idx);
                        MasterMU.Children.Add(TB50X);
                        TB50X.AUF = _auf;
                        break;
                    case "VA":
                        TAB05X = new TAB05X();
                        TAB05X.SetValue(Grid.RowProperty, idx);
                        MasterMU.Children.Add(TAB05X);
                        TAB05X.AUF = _auf;
                        break;
                    case "VW":
                        TAB45X = new TAB45X();
                        TAB45X.SetValue(Grid.RowProperty, idx);
                        MasterMU.Children.Add(TAB45X);
                        TAB45X.AUF = _auf;
                        break;
                    case "PR":
                        TAB55X = new TAB55X();
                        TAB55X.SetValue(Grid.RowProperty, idx);
                        MasterMU.Children.Add(TAB55X);
                        TAB55X.AUF = _auf;
                        break; 
                } 
                idx++;
                if (app.company == "JLT")
                {
                    break;
                }
            }
            if (idx > 0)
            {
                IsMU = true;
            }
        }
    }
}
